<?php

if (isset($_POST['action'])) {
//    define('SUBSIDY', 8);
    $action = $_POST['action'];
    switch ($action) {
        // forget password action
        case 'forgetPassword':
            require_once 'classes/User.php';
            $result = array();
            $user = new User();
            $r = $user->forgetPassword($_POST['user'], $_POST['question'], $_POST['answer']);
            header('Content-Type: application/json');
            switch ($r) {
                case 5:
                    $result = array(
                        'error' => false,
                        'message' => 'An email has been send to your registered mailbox.'
                    );
                    echo json_encode($result);
                    break;
                case 4:
                    $result = array(
                        'error' => true,
                        'message' => 'Something went wrong. Please try again later.'
                    );
                    echo json_encode($result);
                    break;
                case 3:
                    $result = array(
                        'error' => true,
                        'message' => 'Unable to send email. Did you registered a valid email address?'
                    );
                    echo json_encode($result);
                    break;
                case 2:
                    $result = array(
                        'error' => true,
                        'message' => 'Incorrect answer. Please check your answer to make sure it is entered correctly.'
                    );
                    echo json_encode($result);
                    break;
                case 1:
                    $result = array(
                        'error' => true,
                        'message' => 'Incorrect question selected. Please select the question appropriately.'
                    );
                    echo json_encode($result);
                    break;
                case 0:
                    $result = array(
                        'error' => true,
                        'message' => 'User not found. Did you enter the correct username?'
                    );
                    echo json_encode($result);
                    break;
            }
            break;
        // change password action
        case 'changePassword':
            require_once './classes/User.php';
            $result = array();
            $user = new User();
            $r = $user->changePassword();
            header('Content-Type: application/json');
            switch ($r) {
                case 4:
                    $result = array(
                        'error' => false,
                        'message' => 'Password changed successfully.'
                    );
                    echo json_encode($result);
                    break;
                case 3:
                    $result = array(
                        'error' => true,
                        'message' => 'Unable to change password now, please try again later.'
                    );
                    echo json_encode($result);
                    break;
                case 2:
                    $result = array(
                        'error' => true,
                        'message' => 'New password did not match, did you enter the correct password?'
                    );
                    echo json_encode($result);
                    break;
                case 1:
                    $result = array(
                        'error' => true,
                        'message' => 'Password mismatch, did you enter the right password?'
                    );
                    echo json_encode($result);
                    break;
                case 0:
                    $result = array(
                        'error' => true,
                        'message' => 'Something went wrong. Please try again later.'
                    );
                    echo json_encode($result);
                    break;
            }
            break;
        // check password action
        case 'checkPassword':
            require_once 'classes/User.php';
            $u = new User();
            header('Content-Type: application/json');
            if ($u->checkPassword()) {
                $result = array('error' => false);
                echo json_encode($result);
            } else {
                $result = array('error' => true, 'message' => 'Password did not match. Did you enter the password correctly?');
                echo json_encode($result);
            }
            break;
        // register action
        case 'register':
            require_once 'classes/User.php';
            require_once 'classes/Member.php';
            $user = new User();
            $member = new Member();
            header('Content-Type: application/json');
            if ($user->createUser() && $member->createMember($_POST['user'])) {
                $res = array(
                    'error' => false,
                    'message' => 'Register successful. Redirecting you to login page in few seconds.
                            <script>var counter = 3;
                            setInterval(function() {
                            counter--;
                            if (counter < 0) {window.location = "loginPage.php";} }, 1000);</script>'
                );
                echo json_encode($res);
            } else {
                $res = array(
                    'error' => true,
                    'message' => 'Unfortunately something went wrong in the system. Please try again later.'
                );
                echo json_encode($res);
            }
            break;
        // check email action
        case 'checkEmail':
            require_once 'classes/User.php';
            $user = new User();
            $r = $user->checkEmail();
            header('Content-Type: application/json');
            echo json_encode($r);
            break;
        // check username action
        case 'checkUsername':
            require_once 'classes/User.php';
            $user = new User();
            $r = $user->checkUsername();
            header('Content-Type: application/json');
            echo json_encode($r);
            break;
        // check icno action
        case 'checkicno':
            require_once 'classes/Member.php';
            $member = new Member();
            $r = $member->checkIcno();
            header('Content-Type: application/json');
            echo json_encode($r);
            break;
        // update details action
        case 'updateDetails':
            require_once './classes/Member.php';
            $member = new Member();
            header('Content-Type: application/json');
            if ($member->updateDetails()) {
                $result = array(
                    'error' => false,
                    'message' => 'Profile updated successfully.'
                );
                echo json_encode($result);
            } else {
                $result = array(
                    'error' => true,
                    'message' => 'Profile not being updated, please make sure that you have changed the details before submitting.'
                );
                echo json_encode($result);
            }
            break;
    }
} else {
    header('HTTP/1.0 404 Not Found');
    include '404.php';
    exit;
}
?>
